using System;
using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using NewGameTypes;
using RecommendedProduct;
using UnityEngine;

public class SelectTransportUI : MonoBehaviour
{
	public const int COUNT_BUTTONS = 4;

	[SerializeField]
	private GameModesUI controllerSelectMode;

	[SerializeField]
	private SelectableButtonTransport exampleTransportButton;

	[SerializeField]
	private UIScrollView scroll;

	[SerializeField]
	private GameObject scrollBar;

	[SerializeField]
	private UILabel selectTransportLable;

	[SerializeField]
	private UICenterOnChild centerOnChild;

	[SerializeField]
	private MarketMiniProductInfo transportInfoPanel;

	[SerializeField]
	private SelectableButtonController selectableButtonController;

	[SerializeField]
	private SelectableButtonController selectableButtonShopController;

	[SerializeField]
	private SelectableButtonShop exampleShopButton;

	private SelectableButtonShop _boughtShopButton;

	public MarketMiniProductInfo TransportInfoPanel
	{
		get
		{
			return transportInfoPanel;
		}
	}

	public SelectableButtonController SelectableButtonController
	{
		get
		{
			return selectableButtonController;
		}
	}

	public SelectableButtonController SelectableButtonShopController
	{
		get
		{
			return selectableButtonShopController;
		}
	}

	public SelectableButtonShop ExampleShopButton
	{
		get
		{
			return exampleShopButton;
		}
	}

	public SelectableButtonShop BoughtShopButton
	{
		get
		{
			return _boughtShopButton;
		}
		set
		{
			_boughtShopButton = value;
		}
	}

	private void Start()
	{
		centerOnChild.onCenter = DisableCenterOnChild;
	}

	private void OnEnable()
	{
		Reinit();
	}

	public void Reinit()
	{
		ClearScroll();
		StartCoroutine(InitTransportButton());
		StartCoroutine(InitShopButton());
	}

	private IEnumerator InitTransportButton()
	{
		GameModesUI.TransportType transportType = controllerSelectMode.SelectableButtonController.GetSelectableButton().ModeInfo.typeTransport;
		switch (transportType)
		{
		case GameModesUI.TransportType.car:
			selectTransportLable.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Game_Common/gts_selectCar"))) ? ScriptLocalization.Get("Game_Common/gts_selectCar") : "Select car");
			break;
		case GameModesUI.TransportType.tank:
			selectTransportLable.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Game_Common/gts_selectTank"))) ? ScriptLocalization.Get("Game_Common/gts_selectTank") : "Select tank");
			break;
		case GameModesUI.TransportType.titan:
			selectTransportLable.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Game_Common/gts_selectTitan"))) ? ScriptLocalization.Get("Game_Common/gts_selectTitan") : "Select titan");
			break;
		default:
			selectTransportLable.text = "Select transport";
			break;
		}
		int sumWButtonWidth = 0;
		List<ProductCar> transportList = new TransportList().GetActualTransportList(transportType);
		string selectedTransportName = settings.GetSelectedTransportName(transportType);
		bool isSelectedButton = false;
		foreach (ProductCar element in transportList)
		{
			SelectableButtonTransport tmpButton = UnityEngine.Object.Instantiate(exampleTransportButton);
			tmpButton.gameObject.SetActive(true);
			selectableButtonController.AddNewButton(tmpButton);
			tmpButton.transform.SetParent(selectableButtonController.grid.transform);
			tmpButton.transform.localScale = Vector3.one;
			tmpButton.TransportProduct = element;
			tmpButton.TransportType = transportType;
			tmpButton.TransportInfoPanel = transportInfoPanel;
			tmpButton.SelectTransportController = this;
			tmpButton.Picture.mainTexture = null;
			tmpButton.SetName((!string.IsNullOrEmpty(ScriptLocalization.Get("Market/" + element.id + "_title"))) ? ScriptLocalization.Get("Market/" + element.id + "_title") : "Нет локализации");
			ProductVehicleSkin skinProduct = MarketController.Instance.GetEquipedVehicleSkin(element.id);
			if (skinProduct != null)
			{
				tmpButton.LoadAndSetTexture("AtlasShopGuns/", "ASG_" + skinProduct.skinTextureName);
			}
			if (tmpButton.Picture.mainTexture == null)
			{
				tmpButton.LoadAndSetTexture("ModeSelektor/", element.texture);
			}
			if (tmpButton.Picture.mainTexture == null)
			{
				tmpButton.LoadAndSetTexture("AtlasShopGuns/", element.texture);
			}
			if (selectedTransportName.Equals(element.id))
			{
				tmpButton.Select(true);
				isSelectedButton = true;
			}
			sumWButtonWidth += Convert.ToInt32(selectableButtonController.grid.cellWidth);
		}
		if (sumWButtonWidth > base.gameObject.GetComponent<UIWidget>().width)
		{
			scrollBar.SetActive(true);
			scroll.canDragScroll = true;
		}
		else
		{
			scrollBar.SetActive(false);
			scroll.canDragScroll = false;
		}
		if (selectableButtonController.allButtons[0] != null && !isSelectedButton)
		{
			selectableButtonController.allButtons[0].Select(true);
		}
		selectableButtonController.grid.repositionNow = true;
		yield return null;
		CenterOnSelectedButton();
	}

	private IEnumerator InitShopButton()
	{
		TypeGame typeGame = controllerSelectMode.SelectableButtonController.GetSelectableButton().ModeInfo.typeGame;
		List<Product> products = ProductStaticManager.GetInstance().GetProduct(typeGame);
		for (int i = 0; i < selectableButtonShopController.allButtons.Count; i++)
		{
			SelectableButtonShop tmpButton = selectableButtonShopController.allButtons[i] as SelectableButtonShop;
			tmpButton.SelectTransportController = this;
			if (i < products.Count)
			{
				tmpButton.gameObject.SetActive(true);
				tmpButton.InitButton(products[i]);
			}
			else
			{
				tmpButton.gameObject.SetActive(false);
			}
		}
		yield return null;
		selectableButtonShopController.grid.enabled = true;
	}

	private void ClearScroll()
	{
		selectableButtonController.ClearAllButtons();
	}

	private void CenterOnSelectedButton()
	{
		centerOnChild.Recenter();
		scroll.ResetPosition();
		if (selectableButtonController.selectedButton != null)
		{
			int num = selectableButtonController.allButtons.IndexOf(selectableButtonController.selectedButton);
			if (num >= 4)
			{
				centerOnChild.CenterOn(selectableButtonController.selectedButton.transform);
			}
		}
	}

	private void DisableCenterOnChild(GameObject go = null)
	{
		centerOnChild.enabled = false;
	}
}
